#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
int main() {
    int m, n;
    scanf("%d %d", &m, &n);
    int a[10][10];
    getchar();
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            char z;
            scanf("%c", &z);
            if (z == '#')
                a[i][j] = 2;
            else
                a[i][j] = 0;
            printf("%d", a[i][j]);
        }
        getchar();
        printf("\n");
    }
    int k;
    scanf("%d", &k);
    for (int i = 1; i <= k; i++) {
        int x, y;
        int count = 0;
        scanf("%d %d", &x, &y);
        x++;
        y++;
        if (a[x][y] == 2 || a[x][y] == 1);
        {
            a[x][y] = 0;
            count++;
        }
        if (a[x - 1][y] == 2)
            a[x - 1][y] == 1;
        else if (a[x - 1][y] == 1)
        {
            a[x - 1][y] == 0; count++;
        }

        if (a[x + 1][y] == 2)
            a[x + 1][y] == 1;
        else if (a[x + 1][y] == 1)
        {
            a[x + 1][y] == 0; count++;
        }

        if (a[x][y + 1] == 2)
            a[x][y + 1] == 1;
        else if (a[x][y + 1] == 1)
        {
            a[x][y + 1] == 0; count++;
        }

        if (a[x][y - 1] == 2)
            a[x][y - 1] == 1;
        else if (a[x][y - 1] == 1)
        {
            a[x][y - 1] == 0; count++;
        }

        printf("\n");
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                printf("%d", a[i][j]);
            }
            printf("\n");
        }
        printf("%d\n", count);
    }

    return 0;
}